import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import visualizer from 'rollup-plugin-visualizer'
import externalGlobals from 'rollup-plugin-external-globals'
// 全局对象
const externalGlobalsObj = {
  vue: 'Vue',
  'vue-router': 'VueRouter',
  axios: 'axios',
  'vue-demi': 'VueDemi',
  pinia: 'Pinia'
}
// https://vitejs.dev/config/
export default defineConfig({
  base: 'blog/', //资源路径
  plugins: [
    vue(),
    // 自动导入的插件，解析器可以是 vant element and-vue
    Components({
      dts: false,
      // 原因：Toast Confirm 这类组件的样式还是需要单独引入，样式全局引入了，关闭自动引入
      resolvers: [VantResolver({ importStyle: false })]
    }),
    visualizer({
      open: false, // 在生成报告后自动打开报告页面
      gzipSize: true, // 显示 Gzip 压缩后的文件大小
      brotliSize: true // 显示 Brotli 压缩后的文件大小
    }),
    {
      ...externalGlobals(externalGlobalsObj),
      enforce: 'post',
      apply: 'build'
    }
  ],
  build: {
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: ['vue', 'axios', 'vue-router', 'vue-demi', 'pinia']
    },
    //去除日志打印
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: '0.0.0.0',
    proxy: {
      '/api': {
        // target: 'http://127.0.0.1:4003',
        target: 'https://www.fangq.site',
        changeOrigin: true,
        secure: false //绕过安全检测
      }
    }
  }
})
